<template>
    <h3>添加理财信息</h3>
    <table border="1">
        <tr>
            <td>理财名称</td>
            <td>
                <input type="text" v-model="info.LMName" />
            </td>
        </tr>
        <tr>
            <td>年收益</td>
            <td>
                <input type="text" v-model="info.LMYearPrice" />
            </td>
        </tr>
        <tr>
            <td>利息</td>
            <td>
                <select v-model="info.LXId">
                    <option value="">请选择</option>
                    <option v-for="item in Sinfo" :value="item.LXId">{{item.LXYear}}年{{item.LXName}}%</option>
                 
                </select>
            </td>
        </tr>
        <tr>
            <td>最低金额</td>
            <td>
                <input type="text" v-model="info.LMMinPrice" />
            </td>
        </tr>
        <tr>
            <td>理财人次</td>
            <td>
                <input type="text" v-model="info.LMNum" />
            </td>
        </tr>
        <tr>
            <td>风险等级</td>
            <td>
                <select v-model="info.FengId">
                    <option value="">请选择</option>
                    <option v-for="item in Finfo" :value="item.FengId">{{item.FengName}}</option>
                 
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="添加" @click="add()" />
            </td>
           
        </tr>
    </table>
</template>


<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router';

const router=useRouter()

const Sinfo=ref([
    {
        LXId:"",
        LXYear:'',
        LXName:'',
    }
    ])

onMounted(()=>{
    Bang()
    Feng()
})

const info=ref({
    LMName:"",
    LMYearPrice:"",
    LXId:"",
    LMMinPrice:"",
    LMNum:"",
    FengId:"",
})

const Finfo=ref([
    {
        FengId:"",
        FengName:'',
    }
    ])



const Bang=()=>{
    axios.get('http://localhost:2134/api/LMoney/BangLX')
    .then(res=>{
       console.log(res.data)
        Sinfo.value=res.data
    })
}

const Feng=()=>{
    axios.get('http://localhost:2134/api/LMoney/BangDJ')
    .then(res=>{
        Finfo.value=res.data
    })
}

const add=()=>{
    //理财名称不能为空
    if(info.value.LMName==""){
        alert("理财名称不能为空")
        return
    }

    //最低金额必须大于0
    if(Number(info.value.LMMinPrice)<=0){
        alert("最低金额必须大于0")
        return
    }
    axios.post('http://localhost:2134/api/LMoney/AddLMoney',{
        LMName:info.value.LMName,
        LMYearPrice:info.value.LMYearPrice,
        LXId:info.value.LXId,
        LMMinPrice:info.value.LMMinPrice,
        LMNum:info.value.LMNum,
        FengId:info.value.FengId,
    })
    .then(res=>{
        if(res.data>0){
            alert("添加成功")
           location.href="/ShowLMoney"
        }
        else if(res.data==-1){
            alert("理财项目名称不能重复")
        }
        else{
            alert("添加失败")
        }
    })
}

</script>